package com.wb3.organizer.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.wb3.organizer.model.Account;
import com.wb3.organizer.model.Appointment;
import com.wb3.organizer.service.AppointmentService;

public class AppointmentAction extends ActionSupport implements SessionAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static Logger log = Logger.getLogger(AppointmentAction.class);

	private Map<String, Object> session;

	private String view;

	private Appointment appointment;

	private List<Appointment> appointments;

	private AppointmentService service;

	private String operate;

	private String msg;

	public void setSession(Map<String, Object> inSession) {
		this.session = inSession;
	}

	public String getView() {
		return view;
	}

	public void setView(String view) {
		this.view = view;
	}

	public Appointment getAppointment() {
		return appointment;
	}

	public void setAppointment(Appointment appointment) {
		this.appointment = appointment;
	}

	public List<Appointment> getAppointments() {
		return appointments;
	}

	public void setAppointments(List<Appointment> appointments) {
		this.appointments = appointments;
	}

	public AppointmentService getService() {
		return service;
	}

	public void setService(AppointmentService service) {
		this.service = service;
	}

	public String getOperate() {
		return operate;
	}

	public void setOperate(String operate) {
		this.operate = operate;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}


	public String create() {
		log.info("\n\n----------------------------------------------------------\n");
		log.debug("AppointmentAction.create()...\n");
		
		Account account = (Account) session.get("account");
		appointment.setAccount(account);
		appointment.setCreatedDT(new Date().getTime());
		service.saveAppointment(appointment);

		this.setOperate("appointmentCreateOK");
		this.setMsg("计划创建成功!!!");
		log.debug("AppointmentAction.create() Done\n");
		return SUCCESS;
	}
	
	public String update(){
		log.info("\n\n----------------------------------------------------------\n");
		log.debug("AppointmentAction.update()...\n");
		
		Account account = (Account) session.get("account");
		appointment.setAccount(account);
		appointment.setCreatedDT(new Date().getTime());
		service.updateAppointment(appointment);

		this.setOperate("appointmentUpdateOK");
		this.setMsg("计划编辑成功!!!");
		log.debug("AppointmentAction.update() Done\n");
		return SUCCESS;
	}
	
	public String delete(){
		log.info("\n\n----------------------------------------------------------\n");
		log.debug("AppointmentAction.delete()...\n");
		
		service.deleteAppointment(appointment);

		this.setOperate("appointmentDeleteOK");
		this.setMsg("计划删除成功!!!");
		log.debug("AppointmentAction.delete() Done\n");
		return SUCCESS;
	}

	public String list() {
		log.info("\n\n----------------------------------------------------------\n");
		log.debug("AppointmentAction.list()...\n");

		Account account = (Account) session.get("account");
		//如果没有选择视图,默认为day
		if(view.equals("")){
			setView("day");
		}
		appointments = service.getAppointmentByTime(account, new Date(), view);
		log.debug("AppointmentAction.list() Done\n");
		return SUCCESS;
	}

	public String forward() {
		if (operate == null) {
			return SUCCESS;
		} else {
			if (operate.equals("create")) {
				return "create";
			} else if (operate.equals("update")) {
				appointment = service.getAppointment(appointment.getApp_id());
				return "update";
			} else if (operate.equals("delete")) {
				return "delete";
			} else {
				return ERROR;
			}
		}
	}

}
